package com.t174.car.service.impl;

import com.t174.car.dao.CarComponentsDao;
import com.t174.car.dao.CarConfigDao;
import com.t174.car.dao.CarPurchaseDao;
import com.t174.car.dao.CarSalesDao;
import com.t174.car.entity.CarComponents;
import com.t174.car.entity.CarConfig;
import com.t174.car.entity.CarPurchase;
import com.t174.car.entity.CarSales;
import com.t174.car.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class CarImpl implements CarService {
    @Autowired
    CarPurchaseDao carPurchaseDao;
    @Autowired
    CarComponentsDao carComponentsDao ;
    @Autowired
    CarConfigDao carConfigDao ;
    @Autowired
    CarSalesDao carSalesDao;

    @Override
    public int addCar(CarPurchase carPurchase, BigDecimal money) {
        int flag = carPurchaseDao.addCar(carPurchase);//进货记录
        CarComponents carComponents = new CarComponents();
        carComponents.setBrand(carPurchase.getBrand());//品牌
        carComponents.setModel(carPurchase.getModel());//车型
        int brandCount = carComponentsDao.addCar(carComponents);//添加品牌
        CarComponents carComponents1 = carComponentsDao.queryCarBrand(carComponents);//获取添加的品牌ID
        if(carComponents1 == null){
            return -1;
        }else{
            CarConfig carConfig = new CarConfig();
            carConfig.setCarid(carComponents1.getId());
            carConfig.setColor(carPurchase.getColor());
            carConfig.setCount(carPurchase.getCount());
            carConfig.setLevel(carPurchase.getLevel());
            carConfig.setNote(carPurchase.getNote());
            carConfig.setPrice(money);
            return carConfigDao.addCar(carConfig);
        }
    }

    @Override
    public int salesCar(CarSales carSales) {
        int count = -1;
        int carCount = carConfigDao.getCarCount(carSales.getCar());
        if(carCount>=carSales.getCount()){
            //如果库存大于要购买的数量
            count = carConfigDao.salesCar(carSales.getCar(),carSales.getCount());//库存修改
            if(count>0){
                count = carSalesDao.salesCar(carSales);//销售表添加
            }
        }
        return count;
    }
}
